package com.lujianfei.powerinput.ui.noteList.presenter;

import android.content.Intent;
import android.util.Log;
import android.view.View;

import com.lujianfei.powerinput.database.NoteItemFieldModel;
import com.lujianfei.powerinput.database.NoteItemModel;
import com.lujianfei.powerinput.database.TemplateFieldModel;
import com.lujianfei.powerinput.database.TemplateModel;
import com.lujianfei.powerinput.ui.noteList.base.NoteListAddContract;
import com.lujianfei.powerinput.widget.input.base.IWidgetInput;
import com.lujianfei.powerinput.widget.input.factory.InputFactory;

import java.util.ArrayList;
import java.util.List;

public class NoteListAddPresenter implements NoteListAddContract.Presenter {
    private final static String TAG = "NoteListAddPresenter";
    private final NoteListAddContract.View view;
    private NoteItemModel data;
    private TemplateModel templateData;
    private IWidgetInput textInput;

    public NoteListAddPresenter(NoteListAddContract.View view) {
        this.view = view;
    }

    @Override
    public void init() {
        if (null!=data) {
            textInput = InputFactory.newInput("com.lujianfei.powerinput.widget.input.TextInput",view.currentActivity());
            if(null!=textInput) {
                textInput.setWidgetLabel("名称:");
                textInput.setWidgetValue(data.getNoteName());
                view.getContainer().addView(textInput.getWidgetView(), view.getContainer().getChildCount() - 1);

                List<NoteItemFieldModel> noteItemFieldModels = NoteItemFieldModel.find(NoteItemFieldModel.class, "noteItemId=?", "" + data.getId());

                if (null != noteItemFieldModels) {
                    for (NoteItemFieldModel fieldModel :
                            noteItemFieldModels) {
                        IWidgetInput textInputNew = InputFactory.newInput(fieldModel.getClassName(), view.currentActivity());
                        textInputNew.setWidgetLabel(fieldModel.getKey());
                        textInputNew.setWidgetValue(fieldModel.getValue());
                        view.getContainer().addView(textInputNew.getWidgetView(), view.getContainer().getChildCount() - 1);
                    }
                }
            }
        }else if(null!=templateData){
            textInput = InputFactory.newInput("com.lujianfei.powerinput.widget.input.TextInput",view.currentActivity());
            if(null!=textInput) {
                textInput.setWidgetLabel("名称:");
                textInput.setWidgetValue(templateData.getName());
                view.getContainer().addView(textInput.getWidgetView(), view.getContainer().getChildCount() - 1);

                List<TemplateFieldModel> templateFieldModels = TemplateFieldModel.find(TemplateFieldModel.class, "templateId=?", "" + templateData.getId());

                if (null != templateFieldModels) {
                    for (TemplateFieldModel fieldModel :
                            templateFieldModels) {
                        IWidgetInput textInputNew = InputFactory.newInput(fieldModel.getClassName(), view.currentActivity());
                        textInputNew.setWidgetLabel(fieldModel.getKey());
                        view.getContainer().addView(textInputNew.getWidgetView(), view.getContainer().getChildCount() - 1);
                    }
                }
            }
        }
    }

    @Override
    public void onTitleRightImageClick() {
        if (null == data){
            //新增
            NoteItemModel templateModel = new NoteItemModel();
            templateModel.setNoteName(textInput.getWidgetValue());
            long saveId = templateModel.save();
            viewToDb(saveId);
        }else{
            data.setNoteName(textInput.getWidgetValue());
            data.save();
            try {
                //编辑
                List<NoteItemFieldModel> templateFieldModels = NoteItemFieldModel.find(NoteItemFieldModel.class, "noteItemId=?", "" + data.getId());
                //清空原有
                for (NoteItemFieldModel templateFieldModel1 :
                        templateFieldModels) {
                    templateFieldModel1.delete();
                }
            }catch (Exception e){
                Log.e(TAG,""+e);
            }
            viewToDb(data.getId());
        }
        view.currentActivity().finish();
    }
    private List<IWidgetInput> getChilds(){
        int childCount = view.getContainer().getChildCount();
        List<IWidgetInput> widgetInputs = new ArrayList<>();
        for (int i = 0; i < childCount; i++) {
            View childAt = view.getContainer().getChildAt(i);
            if (childAt instanceof IWidgetInput){
                IWidgetInput childAtNew = (IWidgetInput) childAt;
                if(i>0) {
                    widgetInputs.add(childAtNew);
                }
            }
        }
        return widgetInputs;
    }
    private void viewToDb(long noteItemId) {
        List<IWidgetInput> childs = getChilds();
        for (IWidgetInput widgetInput:
                childs) {
            NoteItemFieldModel noteItemFieldModel = new NoteItemFieldModel();
            noteItemFieldModel.setNoteItemId(noteItemId);
            noteItemFieldModel.setClassName(widgetInput.getClass().getName());
            noteItemFieldModel.setKey(widgetInput.getWidgetLabel());
            noteItemFieldModel.setValue(widgetInput.getWidgetValue());
            noteItemFieldModel.setWidgetName(widgetInput.getWidgetName());
            noteItemFieldModel.save();
        }
    }

    @Override
    public void setData(NoteItemModel data) {
        this.data = data;
    }

    @Override
    public void setTemplateData(TemplateModel templateData) {
        this.templateData = templateData;
    }
}
